package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.base.*;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.dao.InventoryStatisticsMapper;
import com.jy.wms.pojo.*;
import io.swagger.models.auth.In;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InventoryStatisticsService extends BaseService<InventoryStatisticsMapper,InventoryStatistics> {
    /**
     * 库存统计报表
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "库存统计报表")
    public Page<InventoryStatistics> stockCount(InventoryStatistics entity)throws ServiceException{
        PageHelper.startPage(entity.getPage(),entity.getRows());
       Page<InventoryStatistics> list= (Page<InventoryStatistics>)dao.stockCount(entity);
       String hpbh="";
       int count=1;
       for(InventoryStatistics statistics:list.getResult()){
           if(hpbh.equals(statistics.getGoodsBh())){
               statistics.setHpdj(count+1);
               count++;
           }else{
               statistics.setHpdj(1);
               hpbh=statistics.getGoodsBh();
               count=1;
           }
       }
       return list;
    }
    /**
     * 导出库存统计报表
     */
    @BaseServiceDescribe(moduleName = "导出报表",btnName = "导出库存统计报表")
    public List<InventoryStatistics> stockCountExport(InventoryStatistics entity){
        List<InventoryStatistics> list = dao.stockCount(entity);
        String hpbh="";
        int count=1;
        for(InventoryStatistics statistics:list){
            if(hpbh.equals(statistics.getGoodsBh())){
                statistics.setHpdj(count+1);
                count++;
            }else{
                statistics.setHpdj(1);
                hpbh=statistics.getGoodsBh();
                count=1;
            }
        }
        return list;
    }
    /**
     * 发运情况分析_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "发运情况分析")
    public Page<ShipmentOrderFx> shipmentOrderFx(Map<String,Object> map)throws ServiceException{
        PageHelper.startPage(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("rows").toString()));
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String cjsj_start="";
        if(map.containsKey("cjsj_start")){
            Date date=new Date(new Long(map.get("cjsj_start").toString()));
             cjsj_start=sdf.format(date);
        }
        String cjsj_end="";
        if(map.containsKey("cjsj_end")){
            Date date2=new Date(new Long(map.get("cjsj_end").toString()));
             cjsj_end=sdf.format(date2);
        }
        String hpmc="";
        if(map.containsKey("hpmc")){
            hpmc=map.get("hpmc").toString();
        }
        String hpbh="";
        if(map.containsKey("hpbh")){
            hpbh=map.get("hpbh").toString();
        }
        Page<ShipmentOrderFx> list= (Page<ShipmentOrderFx>)dao.shipmentOrderFxa(map.get("hzid").toString(),map.get("ckid").toString(),cjsj_start,cjsj_end,hpmc,hpbh);
       for(ShipmentOrderFx fx:list.getResult()){
           Double yf=yfjs(fx.getGoodsId(),map.get("ckid").toString(),map.get("hzid").toString());
            fx.setYf(yf.toString());
            if("0.0".equals(yf.toString())){

                fx.setZb("0:"+fx.getXse().substring(0,fx.getXse().length()-3));
            }else{
                int ed=(int)Double.parseDouble(fx.getXse()+0.5);//销售额
                int yfa=(int)Double.parseDouble(yf.toString()+0.5);//运费
                int gys=GreatestCommonDivisor(yf.toString(),fx.getXse());//公约数
                fx.setZb(ed/gys+":"+yfa/gys);
            }
       }
        return list;
    }
    /**
     * 发运情况分析_Jane
     */
    @BaseServiceDescribe(moduleName = "导出报表",btnName = "导出发运情况分析")
    public List<ShipmentOrderFx> shipmentOrderFxExport(Map<String,Object> map){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String cjsj_start="";
        if(map.containsKey("cjsj_start")){
            Date date=new Date(new Long(map.get("cjsj_start").toString()));
            cjsj_start=sdf.format(date);
        }
        String cjsj_end="";
        if(map.containsKey("cjsj_end")){
            Date date2=new Date(new Long(map.get("cjsj_end").toString()));
            cjsj_end=sdf.format(date2);
        }
        String hpmc="";
        if(map.containsKey("hpmc")){
            hpmc=map.get("hpmc").toString();
        }
        String hpbh="";
        if(map.containsKey("hpbh")){
            hpbh=map.get("hpbh").toString();
        }
        List<String> ids;
        if(map.containsKey("ids")){
          //  ids=map.get("ids").toString();
        }
        List<ShipmentOrderFx> list= dao.shipmentOrderFxa(map.get("hzid").toString(),map.get("ckid").toString(),cjsj_start,cjsj_end,hpmc,hpbh);
        for(ShipmentOrderFx fx:list){
            Double yf=yfjs(fx.getGoodsId(),map.get("ckid").toString(),map.get("hzid").toString());
            fx.setYf(yf.toString());
            if("0.0".equals(yf.toString())){
                fx.setZb("0:"+fx.getXse());
            }else{
                int ed=(int)Double.parseDouble(fx.getXse()+0.5);//销售额
                int yfa=(int)Double.parseDouble(yf.toString()+0.5);//运费
                int gys=GreatestCommonDivisor(yf.toString(),fx.getXse());//公约数
                fx.setZb(ed/gys+":"+yfa/gys);
            }
        }
        return list;
    }
    /**
     * 品项数分析_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "品项数分析表头")
    public JSONObject seachPxsfxTitle(ShipmentOrderFx fx)throws ServiceException{
        JSONObject object=new JSONObject();
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();

        if(fx.getZlids()!=null&&fx.getZlids().size()>0){
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("search","false");
            map.put("minWidth","0px");
            map.put("cRemarks","");
            map.put("prop","zlmc");
            map.put("label","货品种类");
            list.add(map);
        }
        if(fx.getHpids()!=null&&fx.getHpids().size()>0){
            Map<String,Object> map1=new HashMap<String,Object>();
            map1.put("search","false");
            map1.put("minWidth","0px");
            map1.put("cRemarks","");
            map1.put("prop","hpmc");
            map1.put("label","货品名称");
            list.add(map1);
        }
        if(fx.getChanneIds()!=null&&fx.getChanneIds().size()>0){
            Map<String,Object> map2=new HashMap<String,Object>();
            map2.put("search","false");
            map2.put("minWidth","0px");
            map2.put("cRemarks","");
            map2.put("prop","qdmc");
            map2.put("label","销售系统");
            list.add(map2);
        }
        if((fx.getChanneIds()==null||fx.getChanneIds().size()==0)&&(fx.getHpids()==null||fx.getHpids().size()==0)&&(fx.getZlids()==null||fx.getZlids().size()==0)){
            throw new ServiceException("必须选择一个条件才可查询数据");
        }
        Map<String,Object> map3=new HashMap<String,Object>();
        map3.put("search","false");
        map3.put("minWidth","0px");
        map3.put("cRemarks","");
        map3.put("prop","xssl");
        map3.put("label","销售数量");
        list.add(map3);
        Map<String,Object> map4=new HashMap<String,Object>();
        map4.put("search","false");
        map4.put("minWidth","0px");
        map4.put("cRemarks","");
        map4.put("prop","je");
        map4.put("label","单价");
        list.add(map4);
        Map<String,Object> map5=new HashMap<String,Object>();
        map5.put("search","false");
        map5.put("minWidth","0px");
        map5.put("cRemarks","");
        map5.put("prop","xse");
        map5.put("label","销售额");
        list.add(map5);
        Map<String,Object> map6=new HashMap<String,Object>();
        map6.put("search","false");
        map6.put("minWidth","0px");
        map6.put("cRemarks","");
        map6.put("prop","yf");
        map6.put("label","运费");
        list.add(map6);
        Map<String,Object> map7=new HashMap<String,Object>();
        map7.put("search","false");
        map7.put("minWidth","0px");
        map7.put("cRemarks","");
        map7.put("prop","zb");
        map7.put("label","运费/销售额");
        list.add(map7);

        object.put("column",list);
        return object;
    }
    /**
     * 品项数分析_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "品项数分析")
    public Page<ShipmentOrderFx> seachPxsfx(ShipmentOrderFx fx)throws ServiceException{
        PageHelper.startPage(fx.getPage(),fx.getRows());
        Page<ShipmentOrderFx> list= (Page<ShipmentOrderFx>)dao.seachPxsfx(fx);
        for(ShipmentOrderFx orderFx:list.getResult()){
            String yf=yfjs(orderFx.getGoodsId(),orderFx.getCkid(),orderFx.getHzid()).toString();
            orderFx.setYf(yf);
            if("0.0".equals(yf)){
                orderFx.setZb("0:"+orderFx.getXse());
            }else{
                int ed=(int)Double.parseDouble(orderFx.getXse()+0.5);//销售额
                int yfa=(int)Double.parseDouble(yf+0.5);//运费
                int gys=GreatestCommonDivisor(yf,orderFx.getXse());//公约数
                orderFx.setZb(ed/gys+":"+yfa/gys);
            }
        }
        return list;
    }
    /**
     *导出 品项数分析_Jane
     */
    @BaseServiceDescribe(moduleName = "导出报表",btnName = "导出品项数分析")
    public List<ShipmentOrderFx> seachPxsfxExport(ShipmentOrderFx fx){
        List<ShipmentOrderFx> list= dao.seachPxsfx(fx);
        for(ShipmentOrderFx orderFx:list){
            String yf=yfjs(orderFx.getGoodsId(),orderFx.getCkid(),orderFx.getHzid()).toString();
            orderFx.setYf(yf);
            if("0.0".equals(yf)){
                orderFx.setZb("0:"+orderFx.getXse());
            }else{
                int ed=(int)Double.parseDouble(orderFx.getXse()+0.5);//销售额
                int yfa=(int)Double.parseDouble(yf+0.5);//运费
                int gys=GreatestCommonDivisor(yf,orderFx.getXse());//公约数
                orderFx.setZb(ed/gys+":"+yfa/gys);
            }
        }
        return list;
    }
    /**
     * 求最大公约数,计算占比
     */
    public Integer GreatestCommonDivisor(String yf,String xse){
        int ed=(int)Double.parseDouble(xse+0.5);
        int yfa=(int)Double.parseDouble(yf+0.5);
        int remainder=ed%yfa;
        while (remainder!=0){
            ed=yfa;
            yfa=remainder;
            remainder=ed%yfa;
        }
        return yfa;
    }
    /**
     * 计算运费
     */
    public Double yfjs(String hpid,String ckid,String hzid){
        Double zyf=0.00;
        List<ShipmentOrderFx> hpzl=dao.dghpzl(hzid,ckid,hpid);
        for(ShipmentOrderFx fx:hpzl){
            ShipmentOrderFx sozl=dao.sozzl(fx.getSoid());
            if(!"0.00".equals(sozl.getYf())){
               Double zb= fx.getZzl()/sozl.getZzl();
               zyf+=Double.parseDouble(new DecimalFormat("#.00").format(zb*Double.parseDouble(sozl.getYf())));
            }
        }

        return zyf;
    }
    /**
     *查询当前货主下的所有货品种类_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "查询当前货主下的所有货品种类")
    public List<WmsGoodsType> seachHpzl(String ckid,String hzid){
        return dao.seachHpzl(ckid,hzid);
    }
    /**
     *根据种类Id查询货品_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "根据种类Id查询货品")
    public List<WmsGoods> seachHpByZlid(List<String> list){
        return  dao.seachHpByZlid(list);
    }

    /**
     * 查询订单渠道_Jane
     * @param ckid
     * @param hzid
     * @return
     */
//    @BaseServiceDescribe(moduleName = "报表",btnName = "查询订单渠道")
//    public List<WmsOrderChannel> seachDdqd(String ckid,String hzid){
//        return dao.seachDdqd(ckid,hzid,);
//    }
    /**
     * 物流快递表_Jane
     * @param ckid
     * @param hzid
     * @return
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "查询物流快递报表")
    public Page<LogisticsExpress> seachWlkd(String ckid,String hzid,Map<String,Object> map){
        PageHelper.startPage(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("rows").toString()));
        Page<LogisticsExpress> fx=(Page<LogisticsExpress>)dao.seachWlkd(ckid,hzid);
        for(LogisticsExpress orderFx:fx.getResult()){
            if(!"".equals(orderFx.getDdqdid())&&!"0".equals(orderFx.getDdqdid())){
               WmsOrderChannel ddqd=dao.seachDdqd(ckid,hzid,orderFx.getDdqdid());
               if("0".equals(ddqd.getFjd())){
                   orderFx.setYiji(ddqd.getMc());
               }else{
                   WmsOrderChannel ddqd2=dao.seachDdqd(ckid,hzid,ddqd.getFjd());
                   if("0".equals(ddqd2.getFjd())){
                       orderFx.setYiji(ddqd2.getMc());
                       orderFx.setErji(ddqd.getMc());
                   }else{
                       WmsOrderChannel ddqd3=dao.seachDdqd(ckid,hzid,ddqd2.getFjd());
                       if("0".equals(ddqd3.getFjd())){
                           orderFx.setYiji(ddqd3.getMc());
                           orderFx.setErji(ddqd2.getMc());
                           orderFx.setSanji(ddqd.getMc());
                       }
                   }
               }
            }
        }
        return fx;
    }
    /**
     * 物流快递表_Jane
     * @param ckid
     * @param hzid
     * @return
     */
    @BaseServiceDescribe(moduleName = "导出报表",btnName = "导出物流快递报表")
    public List<LogisticsExpress> seachWlkdExport(String ckid,String hzid){
        List<LogisticsExpress> fx=dao.seachWlkd(ckid,hzid);
        for(LogisticsExpress orderFx:fx){
            if(!"".equals(orderFx.getDdqdid())){
                WmsOrderChannel ddqd=dao.seachDdqd(ckid,hzid,orderFx.getDdqdid());
                if("0".equals(ddqd.getFjd())){
                    orderFx.setYiji(ddqd.getMc());
                }else{
                    WmsOrderChannel ddqd2=dao.seachDdqd(ckid,hzid,ddqd.getFjd());
                    if("0".equals(ddqd2.getFjd())){
                        orderFx.setYiji(ddqd2.getMc());
                        orderFx.setErji(ddqd.getMc());
                    }else{
                        WmsOrderChannel ddqd3=dao.seachDdqd(ckid,hzid,ddqd2.getFjd());
                        if("0".equals(ddqd3.getFjd())){
                            orderFx.setYiji(ddqd3.getMc());
                            orderFx.setErji(ddqd2.getMc());
                            orderFx.setSanji(ddqd.getMc());
                        }
                    }
                }
            }
        }
        return fx;
    }
}
